/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.td;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Td设备日志查询
 *
 * @author ShiYun,2014/12/19
 */
public class TdLogService {

    public static final String QUERY = "SELECT * FROM `shj_db`.`td_log` WHERE load_ip=? AND load_user=?";
    public static final String QUERYBYIP = "SELECT * FROM `shj_db`.`td_log` WHERE load_ip=? ORDER BY TIME DESC";
    public static final String QUERYBYUSER = "SELECT * FROM `shj_db`.`td_log` WHERE load_user=? ORDER BY TIME DESC";
    public static final String QUERYALL = "SELECT * FROM `shj_db`.`td_log` ORDER BY TIME DESC";
    public static final String DELETE = "DELETE FROM `shj_db`.`td_log` WHERE load_ip=? AND load_user=?";
    public static final String QUERYTDLOG = "SELECT * FROM `shj_db`.`td_log` WHERE load_ip=? AND load_user=? ORDER BY TIME DESC";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private BaseDao dao = null;

    private void init() {
        dao = new BaseDao();
    }

    private void close() {
        if (dao != null) {
            dao.close();
        }
    }

    public JSONArray queryAll() {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {};
            ResultSet rs = dao.executeSQL(QUERYALL, s);
            while (rs.next()) {
                String id = rs.getString(1);
                long time = rs.getLong(2);
                int type_int = rs.getInt(3);
//                data type: HTTPGET=1 AAA=2 AppFlow=3 AllService=4
                String type = "";
                if (type_int == 1) {
                    type = "HTTPGET";
                }
                if (type_int == 2) {
                    type = "AAA";
                }
                if (type_int == 3) {
                    type = "AppFlow";
                }
                if (type_int == 4) {
                    type = "AllService";
                }
                int fileNums = rs.getInt(4);
                String ip = rs.getString(5);
                String user = rs.getString(6);
                JSONObject jsObj = new JSONObject();
                jsObj.put("id", id);
                jsObj.put("time", sdf.format(time*1000));
                jsObj.put("type", type);
                jsObj.put("files", fileNums);
                jsObj.put("ip", ip);
                jsObj.put("user", user);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TdLogService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }

    public JSONArray queryBYTP(String ip) {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {ip};
            ResultSet rs = dao.executeSQL(QUERYBYIP, s);
            while (rs.next()) {
                String id = rs.getString(1);
                long time = rs.getLong(2);
                int type_int = rs.getInt(3);
//                data type: HTTPGET=1 AAA=2 AppFlow=3 AllService=4
                String type = "";
                if (type_int == 1) {
                    type = "HTTPGET";
                }
                if (type_int == 2) {
                    type = "AAA";
                }
                if (type_int == 3) {
                    type = "AppFlow";
                }
                if (type_int == 4) {
                    type = "AllService";
                }
                int fileNums = rs.getInt(4);
                String user = rs.getString(6);
                JSONObject jsObj = new JSONObject();
                jsObj.put("id", id);
                jsObj.put("time", sdf.format(time*1000));
                jsObj.put("type", type);
                jsObj.put("files", fileNums);
                jsObj.put("ip", ip);
                jsObj.put("user", user);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TdLogService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }

    public JSONArray queryBYUSER(String user) {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {user};
            ResultSet rs = dao.executeSQL(QUERYBYUSER, s);
            while (rs.next()) {
                String id = rs.getString(1);
                long time = rs.getLong(2);
                int type_int = rs.getInt(3);
//                data type: HTTPGET=1 AAA=2 AppFlow=3 AllService=4
                String type = "";
                if (type_int == 1) {
                    type = "HTTPGET";
                }
                if (type_int == 2) {
                    type = "AAA";
                }
                if (type_int == 3) {
                    type = "AppFlow";
                }
                if (type_int == 4) {
                    type = "AllService";
                }
                int fileNums = rs.getInt(4);
                String ip = rs.getString(5);
                JSONObject jsObj = new JSONObject();
                jsObj.put("id", id);
                jsObj.put("time", sdf.format(time*1000));
                jsObj.put("type", type);
                jsObj.put("files", fileNums);
                jsObj.put("ip", ip);
                jsObj.put("user", user);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TdLogService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }

    public boolean delete(String ip, String user) {
        boolean flag = false;
        init();
        Object[] s = {ip, user};
        int num = dao.executeUpdate(DELETE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public JSONArray searchTdLog(String ip, String user) {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {ip, user};
            ResultSet rs = dao.executeSQL(QUERYTDLOG, s);
            while (rs.next()) {
                String id = rs.getString(1);
                long time = rs.getLong(2);
                int type_int = rs.getInt(3);
//                data type: HTTPGET=1 AAA=2 AppFlow=3 AllService=4
                String type = "";
                if (type_int == 1) {
                    type = "HTTPGET";
                }
                if (type_int == 2) {
                    type = "AAA";
                }
                if (type_int == 3) {
                    type = "AppFlow";
                }
                if (type_int == 4) {
                    type = "AllService";
                }
                int fileNums = rs.getInt(4);
                JSONObject jsObj = new JSONObject();
                jsObj.put("id", id);
                jsObj.put("time", sdf.format(time*1000));
                jsObj.put("type", type);
                jsObj.put("files", fileNums);
                jsObj.put("ip", ip);
                jsObj.put("user", user);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TdLogService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }
    
    public static void main(String[] args){
        long t = 1419222960;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("time:"+sdf.format(t*1000));
    }
}
